/**
 * 处理api用户端首页数据
 * @param {*} results
 * @returns
 */

const processedData = (results) => {
  let r = results.reduce((acc, row) => {
    const {
      id,
      name,
      category,
      introduction,
      price,
      location,
      latitude,
      longitude,
      phone,
      rating,
      type,
      is_online,
      createTime,
      updateTime,
    } = row;

    if (!acc[id]) {
      acc[id] = {
        id,
        name,
        category,
        introduction,
        price,
        location,
        latitude,
        longitude,
        phone,
        rating,
        type,
        is_online,
        createTime,
        updateTime,
        media: [],
      };
    }
    // 初始化媒体数组
    let mediaArray = [];

    // 假设每个 row 都有一个 media 字段，我们需要将其添加到媒体数组中
    if (row.media && row.media.length > 0) {
      row.media.forEach((mediaRow) => {
        mediaArray.push({
          id: mediaRow.media_id,
          name: mediaRow.media_name,
          type: mediaRow.media_type,
          path: mediaRow.media_path,
          imgUrl: mediaRow.media_imgUrl,
          is_cover: mediaRow.media_is_cover,
          is_show: mediaRow.media_is_show,
          createTime: mediaRow.media_createTime,
          updateTime: mediaRow.media_updateTime,
        });
      });
    }

    const mediaObj = {
      id: row.media_id,
      name: row.media_name,
      type: row.media_type,
      path: row.media_path,
      imgUrl: row.media_imgUrl,
      is_cover: row.media_is_cover,
      is_show: row.media_is_show,
      createTime: row.media_createTime,
      updateTime: row.media_updateTime,
    };

    acc[id].media.push(mediaObj);
    // 将新对象添加到处理后的数组中

    return acc;
  }, {});

  return r;
};

/**
 * 处理api用户端详情页面媒体数据
 * @param {*} mediaRows
 * @returns
 */
const processMedia = (mediaRows) => {
  let cover = [];
  let images = [];
  let videos = [];

  mediaRows.forEach((item) => {
    if (item.type === "image") {
      // 如果是封面图片，则加入到 imagesCover 数组
      if (item.is_cover === 1) {
        cover.push(item);
      }
      // 如果不是封面图片，则加入到 imagesNormal 数组
      else {
        images.push(item);
      }
    }
    // 如果是视频，则加入到 videos 数组
    else if (item.type === "video") {
      videos.push(item);
    }
  });

  // 返回处理后的数据，包含封面图片、普通图片和视频
  return { cover, images, videos };
};
// 处理api用户端评论页面的数据
const processCommentData = (CommentArr) => {
  const CommentData = [];
  CommentArr.forEach((item) => {
    if (item.food_id) {
      item.type = "food";
      CommentData.push(item);
    } else {
      item.type = "travel";
      CommentData.push(item);
    }
  });
  return CommentData;
};
// 处理首页地图模块数据
const processMapData = (results) => {
  let r = results.reduce((acc, row) => {
    const {
      id,
      name,
      category,
      introduction,
      price,
      location,
      latitude,
      longitude,
      phone,
      rating,
      type,
      is_online,
      createTime,
      updateTime,
    } = row;

    if (!acc[id]) {
      acc[id] = {
        id,
        name,
        category,
        introduction,
        price,
        location,
        latitude,
        longitude,
        phone,
        rating,
        type,
        width: 20, //宽
        height: 30, //高
        is_online,
        createTime,
        updateTime,
        cover: [],
      };
    }
    // 初始化媒体数组
    let mediaArray = [];

    // 假设每个 row 都有一个 media 字段，我们需要将其添加到媒体数组中
    if (row.cover && row.cover.length > 0) {
      row.cover.forEach((mediaRow) => {
        mediaArray.push({
          id: mediaRow.media_id,
          name: mediaRow.media_name,
          type: mediaRow.media_type,
          path: mediaRow.media_path,
          imgUrl: mediaRow.media_imgUrl,
          is_cover: mediaRow.media_is_cover,
          is_show: mediaRow.media_is_show,
          createTime: mediaRow.media_createTime,
          updateTime: mediaRow.media_updateTime,
        });
      });
    }

    const mediaObj = {
      id: row.media_id,
      name: row.media_name,
      type: row.media_type,
      path: row.media_path,
      imgUrl: row.media_imgUrl,
      is_cover: row.media_is_cover,
      is_show: row.media_is_show,
      createTime: row.media_createTime,
      updateTime: row.media_updateTime,
    };

    acc[id].cover.push(mediaObj);
    // 将新对象添加到处理后的数组中

    return acc;
  }, {});

  return r;
};

module.exports = {
  processedData,
  processMedia,
  processCommentData,
  processMapData,
};
